package com.betterjr.modules.risk.loan;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.betterjr.common.web.ControllerExceptionHandler;
import com.betterjr.common.web.Servlets;
import com.betterjr.common.web.ControllerExceptionHandler.ExceptionHandler;

@Controller
@RequestMapping("/Scf/Risk/Loan/LoanProjectRelation")
public class LoanProjectRelationController {

    private static final Logger logger = LoggerFactory.getLogger(LoanProjectRelationController.class);

    @Reference(interfaceClass = ILoanProjectRelationService.class)
    private ILoanProjectRelationService relationService;

    /**
     * 
     * @Title: createRelationByRefNo 
     * @Description: 创建融资贷款项目 
     * @param request
     * @param refNos
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月29日 下午1:43:19
     */
    @RequestMapping(value = "/createRelation", method = RequestMethod.POST)
    public @ResponseBody String createRelationByRefNo(final HttpServletRequest request, String refNos) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("创建融资贷款,入参:" + anMap.toString());
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return relationService.webCreateRelationByRefNo(refNos);
            }
        }, "创建融资贷款项目失败", logger);
    }

    /**
     * 
     * @Title: findLoanProjectReltionDetail 
     * @Description: 查询融资项目
     * @param request
     * @param id
     * @return 
     * @throws 
     * @author xuyp
     * @date 2018年6月29日 下午1:46:46
     */
    @RequestMapping(value = "/findLoanDetail", method = RequestMethod.POST)
    public @ResponseBody String findLoanProjectReltionDetail(final HttpServletRequest request, Long id) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("查询融资贷款,入参:" + anMap.toString());
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return relationService.webFindLoanProjectReltionDetailById(id);
            }
        }, "查询融资贷款项目失败", logger);
    }

    /**
     * 
     * @Title: queryLoanRelations 
     * @Description: 分页查询贷款项目
     * @param request
     * @param orderData
     * @param flag
     * @param pageNum
     * @param pageSize
     * @return 
     * @throws 
     * @author xuyp
     * @date 2018年6月29日 下午1:49:17
     */
    @RequestMapping(value = "/queryRelations", method = RequestMethod.POST)
    public @ResponseBody String queryLoanRelations(final HttpServletRequest request, String orderData, String flag,
            int pageNum, int pageSize) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("查询融资贷款,入参:" + anMap.toString());
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return relationService.webQueryLoanRelations(anMap, orderData, flag, pageNum, pageSize);
            }
        }, "查询融资贷款项目失败", logger);
    }

    /**
     * 
     * @Title: saveDeletedLoanById 
     * @Description: 废止贷款项目
     * @param request
     * @param id
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月29日 下午1:51:06
     */
    @RequestMapping(value = "/cacleRelation", method = RequestMethod.POST)
    public @ResponseBody String saveDeletedLoanById(final HttpServletRequest request, Long id) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("废止融资贷款,入参:" + anMap.toString());
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return relationService.webSaveDeletedLoanById(id);
            }
        }, "废止融资贷款项目失败", logger);
    }

    /**
     * 
     * @Title: saveEffectiveOrStorageLoan 
     * @Description: 创建或者暂存贷款项目
     * @param request
     * @param operatorType
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月29日 下午1:52:36
     */
    @RequestMapping(value = "/addRelation", method = RequestMethod.POST)
    public @ResponseBody String saveEffectiveOrStorageLoan(final HttpServletRequest request, String operatorType,
            String agreementIds) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("创建融资贷款,入参:" + anMap.toString());
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return relationService.webSaveEffectiveOrStorageLoan(anMap, operatorType, agreementIds);
            }
        }, "创建融资贷款项目失败", logger);
    }

}
